import random
import string

# 生成特定长度的随机密码
def generate_password(length, categories):
    password = []
    for _ in range(length):
        # 从提供的字符类别中随机选择一个类别，再随机选择一个字符
        category = random.choice(categories)
        password.append(random.choice(category))
    return ''.join(password)

# 按照指定需求生成一组密码
def generate_passwords(num_passwords):
    passwords = []
    
    # 定义字符类别
    upper = string.ascii_uppercase
    lower = string.ascii_lowercase
    digits = string.digits
    special = "~!@#$%^&*"
    
    for _ in range(num_passwords):
        # 随机选择密码长度，控制部分密码长度小于8或大于16
        length = random.choices([random.randint(1, 7), random.randint(8, 16), random.randint(17, 1000)], weights=[0.1, 0.8, 0.1])[0]
        
        # 随机选择至少三个类别来确保复杂性
        categories = random.sample([upper, lower, digits, special], k=random.randint(1, 4))
        
        # 生成密码并添加到列表
        password = generate_password(length, categories)
        passwords.append(password)
    
    return passwords

def write_to_files(num_files=5):
    for i in range(1, num_files + 1):
        # 随机生成 N 值
        N = random.randint(1, 1000)
        
        # 生成 N 个密码数据
        passwords = generate_passwords(N)
        
        # 文件名为 `i.in`
        filename = f"{i}.in"
        
        # 写入文件
        with open(filename, 'w') as f:
            f.write(f"{N}\n")
            for password in passwords:
                f.write(f"{password}\n")
        
        print(f"测试数据已生成并保存到 {filename} 文件中，包含 {N} 组测试数据。")

# 生成多个 `.in` 文件
write_to_files(num_files=5)
